<?php

declare(strict_types=1);

use Phinx\Seed\AbstractSeed;

class AdminRoleSeeder extends AbstractSeed
{
    /**
     * Run Method.
     */
    public function run(): void
    {
        // 创建超级管理员角色
        $roleTable = $this->table('role');
        $roles = [
            [
                'name' => '超级管理员',
                'description' => '系统超级管理员，拥有所有权限',
                'status' => 1,
                'sort' => 0,
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s'),
            ],
        ];
        $roleTable->insert($roles)->saveData();
        // 获取超级管理员角色ID
        $roleRow = $this->fetchRow("SELECT id FROM role WHERE name = '超级管理员'");
        $roleId = $roleRow['id'];
        // 创建超级管理员账号
        $adminTable = $this->table('admin');
        $admins = [
            [
                'user_name' => 'admin',
                'mobile' => '15200000000',
                'email' => '',
                'password' => password_hash('123456', PASSWORD_DEFAULT), // 默认密码 123456
                'avatar' => null,
                'status' => 1,
                'last_login_time' => null,
                'last_login_ip' => null,
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s'),
            ],
        ];
        $adminTable->insert($admins)->saveData();
        // 获取管理员ID
        $adminRow = $this->fetchRow("SELECT id FROM admin WHERE user_name = 'admin'");
        $adminId = $adminRow['id'];
        // 关联管理员和角色
        $adminRoleTable = $this->table('admin_role');
        $adminRoles = [
            [
                'admin_id' => $adminId,
                'role_id' => $roleId,
                'create_time' => date('Y-m-d H:i:s'),
            ],
        ];
        $adminRoleTable->insert($adminRoles)->saveData();
        echo "超级管理员角色和账号创建成功！\n";
        echo "管理员账号: admin\n";
        echo "默认密码: 123456\n";
        echo "请登录后及时修改密码！\n";
    }
}
